import request from "@/api/index";
interface dataSourceType {
  [propName: string]: any;
}
export const checkNameForm = async (_rule: dataSourceType = {}, value: any) => {
  console.log(_rule, 111);

  if (_rule.required) {
    // 必填项验证非空并排除非必须项
    if (value === "" || value.length === 0) {
      return Promise.reject(`${_rule.msg1}`);
    }
  }
  if (_rule.value && _rule.value == value) {
    // 没变化没校验的必要
    return Promise.resolve();
  }
  if (_rule.api && value) {
    // 没填值没有校验的必要
    var params = {
      name: value,
      type: _rule.params.type,
      code: value,
      id: _rule.params.id,
    };
    let res = await request[`${_rule.api}`](params);
    if (!res) {
      return Promise.reject(`${_rule.msg2}`);
    } else {
      return Promise.resolve();
    }
  }
};
